package com.zr.daoimpl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.zr.dao.EmployeedaoSCM;
import com.zr.model.Deliverypoint;
import com.zr.model.Driver;
import com.zr.model.Employeeinformation;
import com.zr.model.Function;
import com.zr.model.Role;
import com.zr.model.User;
import com.zr.vo.Emp_user;

/**
 * @author  shichongming
 * @E-mail: 
 * @date 创建时间：2017年7月4日 下午8:28:27 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */

@Repository("employeedao")
public class EmployeedaoimplSCM extends BaseDaOImpl implements EmployeedaoSCM{

	/**
	 * 求出员工的总数包括公司员工，配送点管理员，车辆管理员
	 * */
	@Override
	public int findEmployeetTotal() {	
		String hql = "select count(e.eiId) from Employeeinformation e";
		Query query = this.getCurrentSession().createQuery(hql);
		List<Object>  obj = query.list();
		String obj1=obj.get(0).toString();
		int total = Integer.parseInt(obj1);	
		return total;
	}

	
	
	/**
	 * 对公司的全部员工进行分页
	 * */
	@Override
	public JSONArray findEmployeePage(int page,int rows) {	
		
		JSONArray j=new JSONArray();
		String hql = " from Employeeinformation e";			
		Query query = this.getCurrentSession().createQuery(hql);
		query.setFirstResult((page-1)*rows);//开始索引
		query.setMaxResults(rows);//取几条
		List<Employeeinformation> obj = query.list();
		
		Iterator<Employeeinformation> it = obj.iterator();
		while(it.hasNext()) {
			Employeeinformation e=it.next();		
			
			JSONObject js=new JSONObject();
			js.put("eild", e.getEiId());
			js.put("eiName",e.getEiName());
			js.put("eipostion",e.getEiPosition());
			js.put("eiPhone", e.getEiPhone());
			js.put("dpName",e.getDeliverypoint().getDpName());
			js.put("dpGrade",e.getDeliverypoint().getDpGrade());
			js.put("eistate",e.getEistate());
			js.put("uname", e.getUser().getUname());
			js.put("upsw",e.getUser().getUpsw());
			js.put("ustate",e.getUser().getUstate());
			js.put("rname",e.getUser().getRole().getRname());		
			j.add(js);	
		}	
		return j;
		
	}



	/**
	 * 
	 * 展示入职时选择的角色
	 * 
	 * */
	@Override
	public JSONArray findAllRole() {		
		JSONArray j=new JSONArray();
		String hql = " from Role";			
		Query query = this.getCurrentSession().createQuery(hql);
		List<Role> obj = query.list();	
		Iterator<Role> it = obj.iterator();
	while(it.hasNext()){
		Role r=it.next();
		JSONObject js=new JSONObject();
		js.put("id",r.getRid());
		js.put("text",r.getRname());
		j.add(js);
	}
		return j;
	}

	
/**
 * 展示入职时候选择的配送点名称
 * */
	@Override
	public JSONArray findAllDeliverypoint() {
		JSONArray j=new JSONArray();
		String hql = " from Deliverypoint";			
		Query query = this.getCurrentSession().createQuery(hql);
		List<Deliverypoint> obj = query.list();	
		Iterator<Deliverypoint> it = obj.iterator();
	while(it.hasNext()){
		Deliverypoint d=it.next();
		JSONObject js=new JSONObject();
		js.put("id",d.getDpId());
		js.put("text",d.getDpName());
		j.add(js);
	}
		return j;
	}



	
	

	/**
	 * 增加员工
	 * */
	@Override
	public int  addEmployee(String eiName, String eipostion, String eiPhone,
			String uname, String upsw) {
		Employeeinformation e=new Employeeinformation();
		e.setEiName(eiName);
		e.setEiPhone(eiPhone);
	    e.setEiPosition(eipostion);
		e.setEistate("在职");
		User u=new User();
		u.setUname(uname);
		u.setUpsw(upsw);
		u.setUstate("未登录");		
		e.setUser(u);			
		this.getCurrentSession().save(u); 
		Serializable a= this.getCurrentSession().save(e);
		int a1=(int) a;
		return a1;
	
	}


	
	
	/**
	 * 增加员工的角色sql语句
	 * */
	@Override
	public int addEmployeeRole(String uname, String upsw, int rid) {
		int i=0;
		String sql =  "update user set user.rid =:rid where user.uname=:uname and user.upsw=:upsw";
    	Query  query =  this.getCurrentSession().createSQLQuery(sql);
    	query.setInteger("rid",rid);
    	query.setString("uname", uname);
    	query.setString("upsw", upsw); 
    	 i  = query.executeUpdate();
		return i;
	}


	/**
	 * 
	 * 查询出新员工的id号
	 * */
	@Override
	public int findNewEmployeeId(String uname, String upsw) {
		
		String sql =  "select eiId from employeeinformation , user where employeeinformation.uid=user.uid and user.uname=:uname and user.upsw=:upsw";
    	Query  query  =   this.getCurrentSession().createSQLQuery(sql);
    	query.setString("uname",uname);
    	query.setString("upsw",upsw);    	
    	List<Emp_user>  ss = query.setResultTransformer(Transformers.aliasToBean(Emp_user.class)).list();
    	int i=ss.get(0).getEiId();
    System.out.println(i);
		return i;
	}


	/**
	 * 根据新员工的id号增加新员工的配送点名称
	 * 
	 * */
	@Override
	public int addEmployeeDeliverypoint(int dpid, int eiId) {		
		int i=0;
		String sql =  "update employeeinformation set employeeinformation.dpid =:dpid where employeeinformation.eiId=:eiId";
    	Query  query =  this.getCurrentSession().createSQLQuery(sql);
    	query.setInteger("dpid",dpid);
    	query.setInteger("eiId",eiId);
    	 i  = query.executeUpdate();
		return i;
	}



	
/**
 * 
 * 员工离职
 */
	@Override
	public void deleteEmployee(int eiId) {		
		Employeeinformation t = (Employeeinformation)this.getCurrentSession().load(Employeeinformation.class, Integer.valueOf(eiId));		
		t.setEistate("离职");
		t.getUser().setUstate("已登录");	
		
	}


	
	
	
	
	
	
	
	
	
	/** 
	 * 删除员工
	 */
@Override
public int employeeRemove(int eiId) {
	Employeeinformation t = (Employeeinformation)this.getCurrentSession().load(Employeeinformation.class, Integer.valueOf(eiId));
	this.getCurrentSession().delete(t.getUser());
	this.getCurrentSession().delete(t);	
	int i=0;
	/*
	String sql =  "delete from Employeeinformation where Employeeinformation.eiId=:eiId";
	Query  query =  this.getCurrentSession().createSQLQuery(sql);
	query.setInteger("eiId",eiId);	    
	 i  = query.executeUpdate();
	 */
	 return i;
}





/**
 * 
 * 修该员工的基本信息
 * */
	@Override
	public void updateEmployeeinfo(int eild, String eipostion, String eiPhone,
		String upsw, String eistate, String ustate) {	
		
		Employeeinformation t = (Employeeinformation)this.getCurrentSession().load(Employeeinformation.class, Integer.valueOf(eild));
		t.setEiPosition(eipostion);
		t.setEiPhone(eiPhone);
		t.setEistate(eistate);
		t.getUser().setUpsw(upsw);
		t.getUser().setUstate(ustate);	
		
	}


	/***
	 * 修改员工的配送点地名
	 * */
	@Override
	public void updateEmployeedpid(int dpid,int eild) {			
		String sql =  "update employeeinformation set employeeinformation.dpid =:dpid where employeeinformation.eiId=:eiId";
		Query  query =  this.getCurrentSession().createSQLQuery(sql);
		query.setInteger("eiId",eild);	  
		query.setInteger("dpid",dpid);
		query.executeUpdate();	
	}


	
	
	/***
	 * 根据员工的id号查询获取到对应的uid号经行角色的外键修改
	 * */
	@Override
	public int findEmployeeUid(int eild) {			
		Employeeinformation t = (Employeeinformation)this.getCurrentSession().load(Employeeinformation.class, Integer.valueOf(eild));
		return t.getUser().getUid();
	}


	/**
	 * 根据查询出的uid号修改角色外键
	 * */
	@Override
	public void updateEmployeeRid(int uid,int rid) {
		
		String sql =  "update user set user.rid =:rid where user.uid=:uid";
		Query  query =  this.getCurrentSession().createSQLQuery(sql);
		query.setInteger("rid",rid);	  
		query.setInteger("uid",uid);
		query.executeUpdate();
		
	}



	
	
	
	/**
	 * 求出司机总数
	 * */
	@Override
	public int getDrivertotal() {
		String hql = "select count(d.driverId) from Driver d";
		Query query = this.getCurrentSession().createQuery(hql);
		List<Object>  obj = query.list();
		String obj1=obj.get(0).toString();
		int total = Integer.parseInt(obj1);	
		return total;
	
	}


	/**
	 * 展示司机并分页
	 * 
	 * */
	@Override
	public JSONArray findDriver(int page, int rows) {	
		JSONArray j=new JSONArray();
		String hql = " from Driver d";			
		Query query = this.getCurrentSession().createQuery(hql);
		query.setFirstResult((page-1)*rows);//开始索引
		query.setMaxResults(rows);//取几条
		List<Driver> obj = query.list();		
		Iterator<Driver> it = obj.iterator();
		while(it.hasNext()) {
			Driver d=it.next();					
			JSONObject js=new JSONObject();
			js.put("driverId",d.getDriverId());
			js.put("driverName",d.getDriverName());
			js.put("driverPhone",d.getDriverPhone());
			js.put("uname", d.getUser().getUname());
			js.put("upsw",d.getUser().getUpsw());
			js.put("ustate",d.getUser().getUstate());
			js.put("role",d.getUser().getRole().getRname());					
			j.add(js);	
		}		
		return j;
		
	}


	/**
	 * 
	 * 司机入职
	 * */
	@Override
	public int addDriver(String driverName, String driverPhone, String uname,
			String upsw) {		
		Driver d=new Driver();
		d.setDriverName(driverName);
		d.setDriverPhone(driverPhone);
		User u=new User();
		u.setUname(uname);
		u.setUpsw(upsw);
		u.setUstate("未登录");			
		d.setUser(u);			
		this.getCurrentSession().save(u); 
		Serializable a= this.getCurrentSession().save(d);
		int a1=(int) a;
		return a1;		
	}


	/**
	 * 增加司机的角色
	 * */
	@Override
	public int addDriverRole(String uname, String upsw, int rid) {
		int i=0;
		String sql =  "update user set user.rid =:rid where user.uname=:uname and user.upsw=:upsw";
    	Query  query =  this.getCurrentSession().createSQLQuery(sql);
    	query.setInteger("rid",rid);
    	query.setString("uname", uname);
    	query.setString("upsw", upsw); 
    	 i  = query.executeUpdate();
		return i;
	}



	/** 
	 * 司机离职，修改状态
	 * */
	@Override
	public int deleteDriver(int driverId) {
		Driver t = (Driver)this.getCurrentSession().load(Driver.class, Integer.valueOf(driverId));		
		t.getUser().setUstate("已登录");		
		return 0;
	}


	/**
	 * 修改司机角色
	 * */
	@Override
	public int updateDriver(int driverId, String driverPhone, String upsw,
			String ustate) {
		Driver t = (Driver)this.getCurrentSession().load(Driver.class, Integer.valueOf(driverId));
		t.getUser().setUstate(ustate);
		t.getUser().setUpsw(upsw);
		t.setDriverPhone(driverPhone);
		int uid=t.getUser().getUid();				
		return uid;
	}


	/**
	 * 修改司机的角色
	 * */
	@Override
	public int updateDriverRole(int uid, int rid) {
		String sql =  "update user set user.rid =:rid where user.uid=:uid";
    	Query  query =  this.getCurrentSession().createSQLQuery(sql);
    	query.setInteger("rid",rid);
    	query.setInteger("uid",uid);
    	 int i  = query.executeUpdate();
		return i;
	}



	
	


	
    
	
	

}
